package org.test.dao;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.test.model.Article;
import org.test.model.User;

//测试类
public class UserDao {
	private static SqlSessionFactory sessionFactory = null;
	private static Reader reader = null;
	
	static {
		try {
			reader = Resources.getResourceAsReader("MybatisCfg.xml");
			
//			InputStream in = UserDao.class.getClassLoader().getResourceAsStream("MybatisCfg.xml");
//			reader = new BufferedReader(new InputStreamReader(in));
			
			sessionFactory = new SqlSessionFactoryBuilder().build(reader);
			org.apache.ibatis.logging.LogFactory.useLog4JLogging();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static SqlSessionFactory getSession() {
		return sessionFactory;
	}
	
	public static void testCRUD(SqlSession session,IUserDao ud){
//		使用最基本的查询
//		User u = (User)session.selectOne("org.test.model.UserMapper.selectUserByID",2);
//		System.out.println(u);
		
		//查找单个
//		User u = ud.selectUserByID(1);
//		System.out.println(u);
	
	//查找多个
		List<User> users = ud.selectUserByName("%tom%");
		for(User user : users){
			System.out.println(user);
		}
		
		// 测试增加,增删改后，必须提交事务，否则不会写入到数据库.
//		User user = new User();
//		user.setUserName("guodq");
//		user.setUserAge(25);
//		user.setUserAddress("henan xinxiang");
//		ud.addUser(user);
//		session.commit();
		
//		User user = ud.selectUserByID(2);
//		user.setUserName("user2");
//		user.setUserAge(33);
//		user.setUserAddress("user2's home");
		//更新
//		ud.updateUser(user);
		//删除
//		ud.deleteAUser(user);
//		session.commit();
	}
	
	//测试多对一
	public static void testMTO(SqlSession session,IUserDao ud){
		User user = new User();
		user.setId(1);
		List<Article> articles = ud.getArticlesByUserID(user);
//		List<Article> articles = ud.getArticlesByUserID(1);
//		List<Article> articles = ud.getUserArticles(1);
		
		for (Article art : articles) {
			System.out.println(art);
		}
	}
	
	public static int pageSize = 5;
	public static int pageNum = 2;
	public static void testPaging(SqlSession session,IUserDao ud){
		Map map = new HashMap();
		User userpage = new User();
		userpage.setId(1);
		
		map.put("user", userpage);
		map.put("offset", (pageNum-1)*pageSize);
		map.put("pageSize", pageSize);
		
		List<Article> articles = ud.getUserArticleByPage(map);
		for (Article art : articles) {
			System.out.println(art);
		}
	}
	
	public static void testPluginPaging(SqlSession session,IUserDao ud){
		User u = new User();
		u.setId(1);
		List<Article> articleListPage = ud.articleListPage(u);
		for (Article art : articleListPage) {
			System.out.println(art);
		}
		System.out.println(u.getPage().getCurrentPage());
		System.out.println(u.getPage().getTotalPage());
		System.out.println(u.getPage().getTotalResult());
	}
	
	public static void insertIntoArticle(SqlSession session,IUserDao ud ) {
		for (int i = 0; i < 30; i++) {
			User us = new User();
			us.setId(1);
			Article article = new Article();
			article.setUser(us);
			article.setTitle("title" + (i + 10));
			article.setContent("content1" + (i + 10));
			ud.saveArticle(article);
		}
		session.commit();
	}
	
	public static void testDynamicIf(SqlSession session,IUserDao ud){
		User user = new User(1);
		List<Article> articles = ud.dynamicIfTest(user);
		System.out.println("查询结果总数为:"+articles.size());
	}
	
	public static void main(String[] args) {
		SqlSession session = sessionFactory.openSession();
		//使用接口映射
		IUserDao ud = session.getMapper(IUserDao.class);

//		testCRUD(session,ud);
//		testMTO(session,ud);
//		testPaging(session,ud);
//		testPluginPaging(session,ud);
		testDynamicIf(session,ud);
		
		session.close();
	}
}
